---@class HeroResolveItem
HeroResolveItem = Class("ResolveItem")
local t = HeroResolveItem
function t:ctor()
    self.enableClick = true
end
---@param obj GComponent
function t:Bind(obj,updateCallback,maxCheckCallback)
    self.bind = {
        obj = obj,
        itemBtn = obj:GetChild("itemBtn"),
        quality = obj:GetChild("quality"),
        icon = obj:GetChild("icon"),
        num = obj:GetChild("num"),
        reduceBtn = obj:GetChild("ReduceBtn"),
        ctrlReduce = obj:GetController("c1"),
        ctrlMat = obj:GetController("c2"),
        materialNum = obj:GetChild("gain_num"),
        materialIcon = obj:GetChild("FenJieItemIcon")
    }
    self.bind.reduceBtn.onClick:Clear()
    self.bind.itemBtn.onClick:Clear()
    self.bind.reduceBtn.onClick:Add(self.ClickReduce,self)
    self.bind.itemBtn.onClick:Add(self.ClickItem,self)
    self.updateCallback = updateCallback
    self.maxCheckCallback = maxCheckCallback
    self.visible = true
end
function t:EnableClick(enable)
    self.enableClick = enable
end
function t:Refresh(data)
    self.data = data
    self.curNum = self.data.curNum or self.data.num

    self.bind.icon.url = EnumConst.AtlasPath.ItemAtlas..data.icon
    local iconQuality = Config.IconQuality[data.quality];
    if iconQuality == nil then
        iconQuality = Config.IconQuality[#Config.IconQuality];
    end
    self.bind.quality.url = EnumConst.AtlasPath.ICONAtlas..iconQuality;
    self:RefreshNum()
    if self.curNum then
        self.bind.reduceBtn.visible = self.curNum < self.data.num
    else
        self.bind.reduceBtn.visible = false
    end
end
function t:RefreshMaterial(iconPath)
    self.bind.materialIcon.url = EnumConst.AtlasPath.ItemAtlas..iconPath
end
function t:RefreshMaterialNum(num)
    self.bind.materialNum.text = num
end
function t:VisibleMat(visible)
    if visible then
        self.bind.ctrlMat.selectedIndex = 1
    else
        self.bind.ctrlMat.selectedIndex = 0
    end
end
function t:IsMatVisible()
    if self.bind.ctrlMat.selectedIndex == 1 then
        return true
    else
        return false
    end
end
function t:RefreshNum()
    if self.curNum then
        self.bind.num.text = getNumString(self.curNum);
    else
        self.bind.num.text = ""
    end
end
function t:GetNeedResolveNum()
    return self.data.num - self.curNum
end
function t:SetNum(num)
    self.curNum = num
end
function t:Visible(visible)
    self.visible = visible
    self.bind.obj.visible = visible
end
function t:ClickReduce()
    if not self.enableClick then
        return
    end
    self.curNum  = self.curNum + 1
    if self.curNum > self.data.num then
        self.curNum = self.data.num
    end
    self.bind.reduceBtn.visible = self.curNum < self.data.num
    self:RefreshNum()
    if self.updateCallback then
        self.updateCallback(self)
    end
end

function t:ClickItem()
    if not self.enableClick then
        return
    end
    if self.maxCheckCallback and self.maxCheckCallback() then
        if self:GetNeedResolveNum() == 0 then
            return
        end
    end
    if self.curNum == 0 then
        UISysTips.AddMsg("已达到最大数量");
        return;
    end
    self.curNum = self.curNum - 1
    if self.curNum < 0 then
        self.curNum = 0
    end
    self.bind.reduceBtn.visible = true
    self:RefreshNum()
    if self.updateCallback then
        self.updateCallback(self)
    end
end
function t:OnDestroy()
    self.bind.reduceBtn.onClick:Clear()
    self.bind.itemBtn.onClick:Clear()
    table.clear(self.bind)
    self.bind = nil
    self.updateCallback = nil
end
return t